var ctx;
var level = -1;
var url = "";
var menuName = "";
var pId = null;

function setCtx(ctx){
	this.ctx = ctx;
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

function gotoPage(page){
	menuName = ifNull($("#menuName").val());
	url = ifNull($("#url").val());
	level = ifNull($("#menuLevel").val());
	
	//解决ie9以下placeholder与value重复的问题
	if(menuName == $("#menuName").attr("placeholder")){
		menuName = "";
	}
	if(url == $("#url").attr("placeholder")){
		url = "";
	}
	loadContent("/menu/menuList?pNo="+ page +"&mName="+ menuName + "&url="+ url +"&level=" + level);
}

function gotoMenuPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	menuName = ifNull($("#menuName").val());
	url = ifNull($("#url").val());
	level = ifNull($("#menuLevel").val());
	
	//解决ie9以下placeholder与value重复的问题
	if(menuName == $("#menuName").attr("placeholder")){
		menuName = "";
	}
	if(url == $("#url").attr("placeholder")){
		url = "";
	}
	loadContent("/menu/menuList?pNo="+ page +"&mName="+ menuName + "&url="+ url +"&level=" + level);
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

/*function clickLi(level1){
	 $("#select_option1").val(level1);
	
}*/

//条件搜索菜单列表
function getMenuList(){
	menuName = ifNull($("#menuName").val());
	url = ifNull($("#url").val());
	level = ifNull($("#menuLevel").val());
	
	//解决ie9以下placeholder与value重复的问题
	if(menuName == $("#menuName").attr("placeholder")){
		menuName = "";
	}
	if(url == $("#url").attr("placeholder")){
		url = "";
	}
	loadContent("/menu/menuList?pNo=1&mName="+ menuName + "&url="+ url +"&level=" + level);
	
}

var menuLevel = $("#mLevel").val();
if(menuLevel > 1 ){
	$("#parentMenu").show();
}else{
	$("#parentMenu").hide();
}

function deleteByIds(){
	//提示框
	tip("删除成功！");
	
	//确认框
	var submit = function () {  
		//alert("确认！"); 
	};
	var mess = "是否提交以上内容！";
	var sub = "确定";
	var calcle = "返回";
	alertMess(submit,mess,sub,calcle);
	
}

//弹出新增或编辑框
function saveOrUpdate (id,state) {
	if(id != null){
		var msg;
		var url;
		if(state == 1){
			msg = "编辑菜单";
			url =  ctx + "/menu/selectPK?id="+id+"";
			saveOrUpdateBox(msg,url);
		}else{
			layer.msg("当前菜单处于停用状态，不能编辑！",{
			    icon: 3,
			    time: 1000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	}else{
		
		$("#parentMenu").attr("style","display:none");
		msg = "新增菜单";
		url = ctx + "/menu/selectPK?id=0";
		saveOrUpdateBox(msg,url);
		
	}
}

var index;
function saveOrUpdateBox(msg,url){
    //官网欢迎页
	index = layer.open({
        type: 2,
        //skin: 'layui-layer-lan',
        title: msg,
        fix: false,
        shadeClose: true,
        scrollbar: false,
        maxmin: false,
        edit: true,
        closeBtn: 1,
        area: ['500px', '365px'],
        content: url,
        btn: ['确定', '取消']
	    ,yes: function(index, layero){ //或者使用btn1
	        //按钮【按钮一】的回调

	    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
	    	iframeWin.saveOrUpdateMenu();

	    },cancel: function(index){ //或者使用btn2
	        //按钮【按钮二】的回调
	    }
	    });
}

//停用，启用菜单方法
function toggleState(menuId,state){
	//确认框

	var mess = "";
	if(state == 1){
		mess ="请确认将要停用该菜单及其子菜单!"; 
		state = 2;
	}else{
		mess ="请确认将要启用该菜单!"; 
		state = 1;
	}
	
	//icon: 1表示正确 2 删除 3 提示
	 layer.confirm(mess, {icon: 3,title:'提示'}, function(index){
		    //do something
			$.get(ctx + "/menu/updateMenuStateById",{
				menuId: menuId,
				state: state
			},function(data){
				data = JSON.parse(data);
				if(data.success){
					layer.msg("操作成功",{
					    icon: 1,
					    time: 1000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
					getMenuList();
					}
					);
				}else{
					layer.msg("操作失败",{
					    icon: 2,
					    time: 1000 //2秒关闭（如果不配置，默认是3秒）
					});
				}
			});
		    layer.close(index);
		}); 
	
}  

function setBgColor(_parent,_child){
    for (var i=0; i<_parent.options.length; i++) {
    	//遍历所有选项
    	             if (_parent.options[i] == _child) {
    	                 _parent.options[i].style.backgroundColor = '#b1e5e5'; 
    	//背景色
    	             } else {
    	                 _parent.options[i].style.backgroundColor = '';             
    	//取消背景色
    	             }
    	        }
}